/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myjme.appstate;

import Globals.AllGlobal;
import brain.MyBrain;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.collision.CollisionResults;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.input.InputManager;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.ui.Picture;
import controller.MyController;
import database.operate.OperateMyDatabaseImpl;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.ZuanKong_Model;

/**
 *
 * @author Administrator
 */
public class GuiAppState extends AbstractAppState{
    /**
     * 创建私有zuankong模型
     */
    private ZuanKong_Model myzuanKong_Model=new ZuanKong_Model();
    private MyBrain brain;
    private MyController controller;
    private SimpleApplication app;
    private Camera cam;
    private Node rootNode;
    private Node guiContainNode=new Node();
    private AssetManager assetManager;
    private Ray ray = new Ray();
    private Node guiNode;
    private InputManager inputManager;
    private static Box mesh = new Box(Vector3f.ZERO, 1, 1, 1);
    private String former_pos=null;
    //
    private BitmapText konghaoBitmapText;
    private BitmapText fangweiBitmapText;
    private BitmapText qingjiaoBitmapText;
    private BitmapText chengkongBitmapText;
    private BitmapText zhujiangqianBitmapText;
    private BitmapText shuiyaBitmapText;
    private BitmapText kongshenBitmapText;
    private BitmapText vBitmapText;
    private BitmapText niantuBitmapText;
    private BitmapText shuiniBitmapText;
    private BitmapText zhongyaBitmapText;
    private BitmapFont guiFont;
    private BitmapText beizhuBitmapText;
    @Override
    public void initialize(AppStateManager stateManager, final Application app) {
        super.initialize(stateManager, app);
        this.app          = (SimpleApplication) app;
        this.cam          = this.app.getCamera();
        this.rootNode     = this.app.getRootNode();
        this.assetManager = this.app.getAssetManager();
        this.guiNode      = this.app.getGuiNode();
        this.inputManager = this.app.getInputManager();
        guiFont=this.assetManager.loadFont("Interface/mychinese/myfont.fnt");
        konghaoBitmapText=new BitmapText(guiFont);
        fangweiBitmapText=new BitmapText(guiFont);
        qingjiaoBitmapText=new BitmapText(guiFont);
        chengkongBitmapText=new BitmapText(guiFont);
        zhujiangqianBitmapText=new BitmapText(guiFont);
        shuiyaBitmapText=new BitmapText(guiFont);
        kongshenBitmapText=new BitmapText(guiFont);
        vBitmapText=new BitmapText(guiFont);
        niantuBitmapText=new BitmapText(guiFont);
        shuiniBitmapText=new BitmapText(guiFont);
        zhongyaBitmapText=new BitmapText(guiFont);
        beizhuBitmapText=new BitmapText(guiFont);
        //下面是文字显示
        konghaoBitmapText.setText(myzuanKong_Model.getMingcheng());
        konghaoBitmapText.setColor(ColorRGBA.Black);
        konghaoBitmapText.setSize(20);
        konghaoBitmapText.move(279, 54, 0);
        guiNode.attachChild(konghaoBitmapText);
        
        fangweiBitmapText.setText(myzuanKong_Model.getFangwei());
        fangweiBitmapText.setColor(ColorRGBA.Black);
        fangweiBitmapText.setSize(20);
        fangweiBitmapText.move(380, 54, 0);
        guiNode.attachChild(fangweiBitmapText);
        qingjiaoBitmapText.setText(myzuanKong_Model.getQingjiao());
        qingjiaoBitmapText.setColor(ColorRGBA.Black);
        qingjiaoBitmapText.setSize(20);
        qingjiaoBitmapText.move(475, 54, 0);
        guiNode.attachChild(qingjiaoBitmapText);
        chengkongBitmapText.setText(myzuanKong_Model.getChengkong());
        chengkongBitmapText.setColor(ColorRGBA.Black);
        chengkongBitmapText.setSize(20);
        chengkongBitmapText.move(570, 54, 0);
        guiNode.attachChild(chengkongBitmapText);
        zhujiangqianBitmapText.setText(myzuanKong_Model.getZhujiangqian());
        zhujiangqianBitmapText.setColor(ColorRGBA.Black);
        zhujiangqianBitmapText.setSize(20);
        zhujiangqianBitmapText.move(677, 54, 0);
        guiNode.attachChild(zhujiangqianBitmapText);
        shuiyaBitmapText.setText(myzuanKong_Model.getShuiya());
        shuiyaBitmapText.setColor(ColorRGBA.Black);
        shuiyaBitmapText.setSize(20);
        shuiyaBitmapText.move(778, 54, 0);
        guiNode.attachChild(shuiyaBitmapText);
        kongshenBitmapText.setText(myzuanKong_Model.getKongshen());
        kongshenBitmapText.setColor(ColorRGBA.Black);
        kongshenBitmapText.setSize(20);
        kongshenBitmapText.move(870, 54, 0);
        guiNode.attachChild(kongshenBitmapText);
        vBitmapText.setText(myzuanKong_Model.getVa());
        vBitmapText.setColor(ColorRGBA.Black);
        vBitmapText.setSize(20);
        vBitmapText.move(279, 36, 0);
        guiNode.attachChild(vBitmapText);
        niantuBitmapText.setText(myzuanKong_Model.getNiantu());
        niantuBitmapText.setColor(ColorRGBA.Black);
        niantuBitmapText.setSize(20);
        niantuBitmapText.move(380, 36, 0);
        guiNode.attachChild(niantuBitmapText);
        shuiniBitmapText.setText(myzuanKong_Model.getShuini());
        shuiniBitmapText.setColor(ColorRGBA.Black);
        shuiniBitmapText.setSize(20);
        shuiniBitmapText.move(475, 36, 0);
        guiNode.attachChild(shuiniBitmapText);
        zhongyaBitmapText.setText(myzuanKong_Model.getZhongya());
        zhongyaBitmapText.setColor(ColorRGBA.Black);
        zhongyaBitmapText.setSize(20);
        zhongyaBitmapText.move(570, 36, 0);
        guiNode.attachChild(zhongyaBitmapText);
        beizhuBitmapText.setText(myzuanKong_Model.getBeizhu());
        beizhuBitmapText.setColor(ColorRGBA.Black);
        beizhuBitmapText.setSize(20);
        beizhuBitmapText.move(667, 36, 0);
        guiNode.attachChild(beizhuBitmapText);
        
        guiNode.attachChild(guiContainNode);
        //
        inputManager.addMapping("SelectObj",
            new MouseButtonTrigger(MouseInput.BUTTON_RIGHT)); 
        ActionListener actionListenerGui = new ActionListener(){
               public void onAction(String name, boolean pressed, float tpf){
                   if(name.equals("SelectObj")&&pressed){
                        CollisionResults results=new CollisionResults();
                        Vector2f click2d=inputManager.getCursorPosition();
                        Vector3f click3d=cam.getWorldCoordinates(new Vector2f(click2d.getX(), click2d.getY()), 0f);
                        Vector3f dir=cam.getWorldCoordinates(new Vector2f(click2d.getX(), click2d.getY()), 1f).subtractLocal(click3d);
                        Ray ray=new Ray(click3d, dir);
                        rootNode.collideWith(ray, results);
                        if(results.size()>0){
                            try {
                                Geometry target=results.getClosestCollision().getGeometry();
                                if(controller!=null){
                                    controller.setBianjizt("当前未处于编辑");
                                    controller.setGuangbiaoxz("光标选中了"+target.getLocalTranslation());
                                    controller.setZhuangtai("当前正在编辑"+target.getName());
                                }
                                ArrayList<ZuanKong_Model> zkmslist=
                                        OperateMyDatabaseImpl.getOne(AllGlobal.ZUANKONG_SQL,
                                        "select * from zuankong where mingcheng='"+target.getName()+"';");
                                if(zkmslist.size()>0)
                                    myzuanKong_Model=zkmslist.get(0);

                                //注意这里还有没有完成的更新3DguiNode
                                updateTheGuiNode(true);
                                
                                controller.getFxStatus().updateStatus("钻场："+myzuanKong_Model.getZuanchang(), "正在选择物体："+target.getName(),target.getLocalTransform().toString(), "未处于编辑状态");
                                SimpleApplication app1=(SimpleApplication)app;
                                Spatial spatial=app1.getGuiNode().getChild(myzuanKong_Model.getZuanchang());
                                if(spatial!=null){
                                    Picture pic=(Picture)spatial;
                                    pic.setImage(assetManager, "Interface/littlemap/pos_zc.gif", false);
                                    if(former_pos!=null){
                                        Spatial spatial_formal=app1.getGuiNode().getChild(former_pos);
                                        Picture pic_formal=(Picture)spatial_formal;
                                        String zcm;
                                        if(former_pos.equals(myzuanKong_Model.getZuanchang())){
                                        }else{
                                            pic_formal.setImage(assetManager, "Interface/littlemap/zc.gif", false);
                                        }
                                        
                                    }
                                    former_pos=myzuanKong_Model.getZuanchang();
                                }                                
                            } catch (Exception ex) {
                                Logger.getLogger(GuiAppState.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                   }
               }
        };

        
        /** Add an action to one or more listeners */
        inputManager.addListener(actionListenerGui, "SelectObj");
    }

    @Override
    public void update(float tpf) {
        super.update(tpf); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void cleanup() {
        super.cleanup(); //To change body of generated methods, choose Tools | Templates.ui
        while(guiContainNode.getChildren().size()>0){
            guiContainNode.getChild(guiContainNode.getChildren().size()-1).removeFromParent();
        }
    }
        /**
     * 更新3D视图的内容
     */
    public void drawInfoPic(){
        Picture  statusPic=new Picture("status");
        statusPic.setImage(assetManager, "Interface/status_new.png", false);
        statusPic.move(240, 4, -3);
        statusPic.setWidth(770);
        statusPic.setHeight(60);
        guiContainNode.attachChild(statusPic);
    }
    public void updateTheGuiNode(boolean  flag){
        if(flag){
            konghaoBitmapText.setText(myzuanKong_Model.getMingcheng());
            fangweiBitmapText.setText(myzuanKong_Model.getFangwei());
            qingjiaoBitmapText.setText(myzuanKong_Model.getQingjiao());
            chengkongBitmapText.setText(myzuanKong_Model.getChengkong());
            zhujiangqianBitmapText.setText(myzuanKong_Model.getZhujiangqian());
            shuiyaBitmapText.setText(myzuanKong_Model.getShuiya());
            kongshenBitmapText.setText(myzuanKong_Model.getKongshen());
            vBitmapText.setText(myzuanKong_Model.getVa());
            niantuBitmapText.setText(myzuanKong_Model.getNiantu());
            shuiniBitmapText.setText(myzuanKong_Model.getShuini());
            zhongyaBitmapText.setText(myzuanKong_Model.getZhongya());
            beizhuBitmapText.setText(myzuanKong_Model.getBeizhu());
        }else{
            konghaoBitmapText.setText("");
            fangweiBitmapText.setText("");
            qingjiaoBitmapText.setText("");
            chengkongBitmapText.setText("");
            zhujiangqianBitmapText.setText("");
            shuiyaBitmapText.setText("");
            kongshenBitmapText.setText("");
            vBitmapText.setText("");
            niantuBitmapText.setText("");
            shuiniBitmapText.setText("");
            zhongyaBitmapText.setText("");
            beizhuBitmapText.setText("");
        }
        
    }
    public void addBrain(MyBrain brain){
        this.brain=brain;
    }
    public void addController(MyController controller){
        this.controller=controller;
        Vector3f v=new Vector3f();
        
    }
  
}
